package com.example.calculadora;

import android.location.GpsStatus.Listener;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends Activity implements OnClickListener {
	String operacion = "";
	String aux1 = "";
	String aux2 = "";
	String num1, num2;
	double solucion;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		EditText texto;

		Button boton01 = (Button) findViewById(R.id.boton01);
		boton01.setOnClickListener(this);
		Button boton02 = (Button) findViewById(R.id.boton02);
		boton02.setOnClickListener(this);
		Button boton03 = (Button) findViewById(R.id.boton03);
		boton03.setOnClickListener(this);
		Button boton04 = (Button) findViewById(R.id.boton04);
		boton04.setOnClickListener(this);
		Button boton05 = (Button) findViewById(R.id.boton05);
		boton05.setOnClickListener(this);
		Button boton06 = (Button) findViewById(R.id.boton06);
		boton06.setOnClickListener(this);
		Button boton07 = (Button) findViewById(R.id.boton07);
		boton07.setOnClickListener(this);
		Button boton08 = (Button) findViewById(R.id.boton08);
		boton08.setOnClickListener(this);
		Button boton09 = (Button) findViewById(R.id.boton09);
		boton09.setOnClickListener(this);
		Button boton00 = (Button) findViewById(R.id.boton00);
		boton00.setOnClickListener(this);
		Button botonMas = (Button) findViewById(R.id.botonMas);
		botonMas.setOnClickListener(this);
		Button botonMenos = (Button) findViewById(R.id.botonMenos);
		botonMenos.setOnClickListener(this);
		Button botonDividir = (Button) findViewById(R.id.botonDividir);
		botonDividir.setOnClickListener(this);
		Button botonMultiplicar = (Button) findViewById(R.id.botonMultiplicar);
		botonMultiplicar.setOnClickListener(this);
		Button botonPunto = (Button) findViewById(R.id.botonPunto);
		botonPunto.setOnClickListener(this);
		Button botonIgual = (Button) findViewById(R.id.botonIgual);
		botonIgual.setOnClickListener(this);

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public void onClick(View v) {
		Button aux = (Button) v;
		EditText text = (EditText) findViewById(R.id.Texto);
		if (isOperacion("" + aux.getText())) {
			operacion = "" + aux.getText();
			// text.setText(operacion);
		}

		if (operacion.equals("")) {
			aux1 += aux.getText();
			text.setText(aux1);

		} else {
			if (aux.getText().equals("+") || aux.getText().equals("=")
					|| aux.getText().equals("-") || aux.getText().equals("*")
					|| aux.getText().equals("/")) {
			} else {
				aux2 += aux.getText();
				text.setText(aux2);
			}

			if (aux.getText().equals("=")) {
				if (operacion.equals("+")) {
					solucion = (Double.parseDouble(aux1))
							+ (Double.parseDouble(aux2));
					text.setText("" + solucion);
					aux2 = "";
				}
				if (operacion.equals("-")) {
					solucion = (Double.parseDouble(aux1))
							- (Double.parseDouble(aux2));
					text.setText("" + solucion);
					aux2 = "";
				}
				if (operacion.equals("*")) {
					solucion = (Double.parseDouble(aux1))
							* (Double.parseDouble(aux2));
					text.setText("" + solucion);
					aux2 = "";
				}
				if (operacion.equals("/")) {
					solucion = (Double.parseDouble(aux1))
							/ (Double.parseDouble(aux2));
					text.setText("" + solucion);
					aux2 = "";
				}

				operacion = "";

			}

			if (!operacion.isEmpty() && solucion != 0) {
				aux1 = (Double.toString(solucion));
				System.out.println("ENTROOOOO");

				if (isOperacion("" + aux.getText())) {
					operacion = "" + aux.getText();
					// text.setText(operacion);
					System.out.println("OPERACION NEW " + operacion);
					// aux2 = "";

				}
			}

		}

		System.out.println("Aux 1 " + aux1);
		System.out.println("Aux 2 " + aux2);
		System.out.println("Operacion " + operacion);
		System.out.println("Aux " + aux.getText());
		System.out.println("Solucion " + solucion);

	}

	public static boolean isOperacion(String s) {
		if (s.equalsIgnoreCase("+"))
			return true;
		if (s.equalsIgnoreCase("-"))
			return true;
		if (s.equalsIgnoreCase("*"))
			return true;
		if (s.equalsIgnoreCase("/"))
			return true;

		else
			return false;

	}
}
